package xzr.Test3;

import java.util.Calendar;
import java.util.GregorianCalendar;

class Person{
    private String name;
    private Calendar birthday;
    private String gender;
    private Person partner;

    public Person(String name, Calendar birthday, String gender, Person partner) {
        this.name = name;
        this.birthday = birthday;
        this.gender = gender;
        this.partner = partner;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Calendar getBirthday() {
        return birthday;
    }

    public void setBirthday(Calendar birthday) {
        this.birthday = birthday;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Person getPartner() {
        return partner;
    }

    public void setPartner(Person partner) {
        this.partner = partner;
    }

    public boolean marry(Person p) {
        if (this.gender == p.gender) {
            System.out.println("同性别，不可以结婚");
            return false;
        } else if (this.partner != null || p.partner != null) {
            System.out.println("一方已婚，不可以结婚");
            return false;
        } else if ("女".equals(this.gender)) {
            if (this.getAge() < 20) {
                System.out.println("女方年龄小于20，不可以结婚");
                return false;
            } else if (p.getAge() < 22) {
                System.out.println("男方年龄小于22，不可以结婚");
                return false;
            }
        } else if ("男".equals(this.gender)) {
            if (this.getAge() < 22) {
                System.out.println("男方年龄小于22，不可以结婚*");
                return false;
            } else if (p.getAge() < 22) {
                System.out.println("女方年龄小于20，不可以结婚*");
                return false;
            }

        }
        return true;
    }

    public int getAge() {//获得年龄的方法
        Calendar today = new GregorianCalendar();//当前的日期
        int todayYear = today.get(Calendar.YEAR);
        int todayMonth = today.get(Calendar.MONTH);
        int todayDay = today.get(Calendar.DAY_OF_MONTH);
        int birthYear = birthday.get(Calendar.YEAR);
        int birthMonth = birthday.get(Calendar.MONTH);
        int birthDay = birthday.get(Calendar.DAY_OF_MONTH);
        if (todayMonth > birthMonth) {
            return todayYear - birthYear;
        } else if (todayMonth < birthMonth) {
            return todayYear - birthYear - 1;
        } else if (todayDay >= birthDay) {
            return todayYear - birthYear;
        } else {
            return todayYear - birthYear - 1;
        }
    }


}
public class Test4 {
    public static void main(String[] args) {
        Calendar man = Calendar.getInstance();
        man.set(1999,3,12);
        Person p = new Person("李",man,"男",null);
        Calendar woman = Calendar.getInstance();
        woman.set(1999,11,21);
        Person p2 = new Person("王",woman,"女",null);

        if(p.marry(p2)){
            System.out.println("成功结婚！");
        }

    }
}
